import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const email = ref('')
    const avatar = ref('')
    const username = ref('')
    const roles = ref([])
    const token = ref('')
    const type = ref('')
    const expires = ref(-1)

    const setBasicInfo = (newEmail, newAvatar, newToken, newType, newExpires) => {
      email.value = newEmail
      avatar.value = newAvatar
      token.value = newToken
      type.value = newType
      expires.value = newExpires
    }
    const setFullInfo = (newUsername, newRoles, newExpires) => {
      username.value = newUsername
      roles.value = newRoles
      expires.value = newExpires
    }
    const refreshTokenInfo = (token, type, expires) => {
      token.value = token
      type.value = type
      expires.value = expires
    }
    const removeFullInfos = () => {
      email.value = ''
      avatar.value = ''
      username.value = ''
      roles.value = []
      token.value = ''
      type.value = ''
      expires.value = -1
    }

    const setAvatar = newAvatar => {
      avatar.value = newAvatar
    }

    return {
      email,
      avatar,
      username,
      roles,
      token,
      type,
      expires,
      setBasicInfo,
      setFullInfo,
      removeFullInfos,
      refreshTokenInfo,
      setAvatar
    }
  },
  { persist: true }
)
